package com.mineplatform.mapper;

import java.util.ArrayList;
import java.util.List;

import com.mineplatform.domain.ReplyContainsUserInfo;
import com.mineplatform.domain.ReplyContainsUserInfoWithMine;
import com.mineplatform.dto.rep.RepliesItemRep;
import com.mineplatform.dto.rep.RepliesItemRep2;
import com.mineplatform.dto.rep.RepliesItemRep3;

public class RepliesItemRepMapper2 {

	public static List<RepliesItemRep2> parse(List<ReplyContainsUserInfoWithMine> replies) {

		List<RepliesItemRep2> list = new ArrayList<RepliesItemRep2>();

		for (ReplyContainsUserInfoWithMine reply : replies) {

			RepliesItemRep2 rep2 = find(reply.getMineId(), list);

			if (rep2 == null) {
				rep2 = new RepliesItemRep2();

				rep2.setMineid(reply.getMineId());

				rep2.setMinetitle(reply.getMineTitle());
				rep2.setMinename(reply.getMineName());
				rep2.setMinesubtitle(reply.getMineSubTitle());
				rep2.setMinecreatetime(reply.getMineCreateTime());
				rep2.setMineimgpath(reply.getMineImgPath());
				rep2.setMineremark(reply.getMineRemark());

				rep2.setReplies(new ArrayList<RepliesItemRep3>());
				list.add(rep2);
			}

			List<RepliesItemRep3> rep3List = rep2.getReplies();

			RepliesItemRep3 rep3 = new RepliesItemRep3();

			rep3.setReplyid(reply.getId());

			rep3.setAvatar(reply.getUserAvatar());
			rep3.setContent(reply.getReplyText());
			rep3.setNickname(reply.getUserNickName());
			rep3.setPubtime(reply.getReplyReportTime());
			rep3.setUserid(rep3.getUserid());

			rep3List.add(rep3);

			// list.add(rep2);

			// rep.setReplyid(reply.getId());
			// rep.setUserid(reply.getUserId());
			// rep.setMineid(reply.getMineId());
			//
			// rep.setPubtime(reply.getReplyReportTime());
			// rep.setContent(reply.getReplyText());
			// rep.setNickname(reply.getUserNickName());
			// rep.setAvatar(reply.getUserAvatar());

		}

		return list;
	}

	public static RepliesItemRep2 find(String key, List<RepliesItemRep2> list) {

		RepliesItemRep2 result = null;
		for (RepliesItemRep2 item : list) {

			if (item.getMineid().equals(key)) {
				result = item;
				break;
			}

		}
		return result;
	}
}
